import Vue from 'vue'
import VueRouter from 'vue-router'
import login from '@/views/user/login.vue'
import home from '@/views/home/home.vue'
import movie from '@/views/movie/movie.vue'
import notfound from '@/views/notfound/notfound.vue'
import qa from '@/views/qa/qa.vue'
import user from '@/views/user/user.vue'
import edit from '@/views/user/edit.vue'
Vue.use(VueRouter)

const routes = [
  {
    name: 'login',
    path: '/login',
    component: login
  },
  {
    name: 'home',
    path: '/home',
    component: home,
    meta: { showTab: true }
  },
  {
    name: 'movie',
    path: '/movie',
    component: movie,
    meta: { showTab: true }
  },

  {
    name: 'qa',
    path: '/qa',
    component: qa,
    meta: { showTab: true }
  },

  {
    name: 'user',
    path: '/user',
    component: user,
    meta: { showTab: true, isLogin: true }
  },

  {
    name: 'edit',
    path: '/edit',
    component: edit,
  },

  {
    name: 'notfound',
    path: '/notfound',
    component: notfound,

  },



  {

    path: '/',
    redirect: '/home'
  },

  {

    path: '*',
    redirect: '/notfound'
  },


]
// 实例化路由对象
const router = new VueRouter({
  routes
})
// 导入token工具包
import { getToken } from '@/utils/token.js'
// 注册导航守卫（全局前置守卫 跳转之前执行）
router.beforeEach((to, from, next) => {
  // console.log(getToken)
  // 判断用户有没有登录
  if (to.meta.isLogin && !getToken()) {
    // 没有登录，则跳转登录页
    next({ path: '/login' })
  } else {
    // 有登录，正常跳转
    next()
  }
})
export default router
